//
//  KPButton.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/3/21.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit

public enum KPButtonPosition {
    case top
    case left
    case right
    case bottom
}

class KPButton: UIButton {

    private struct AssociatedKeys {
        static var anyDataKey = "anyData"
    }

    var titleImageSpacing: CGFloat = 0 { /// 设置图片文字间距
        didSet {
            setNeedsLayout()
        }
    }

    var imageCornerRadius: CGFloat = 0 { /// 设置图片圆角
        didSet {
            imageView?.layer.cornerRadius = imageCornerRadius
            imageView?.layer.masksToBounds = true
        }
    }

    var imageSize: CGSize? { /// 设置图片大小
        didSet {
            setNeedsLayout()
        }
    }

    var imagePosistion: KPButtonPosition = .left { ///图片位置
        didSet {
            setNeedsLayout()
        }
    }

    var anyData: Any? {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.anyDataKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.anyDataKey)
        }
    }

    fileprivate var image: UIImage?

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareView()
    }

    convenience init(image: UIImage?, title: String?, color: UIColor?) {
        self.init(frame: .zero)
        self.image = image
        setTitle(title, for: .normal)
        setImage(image, for: .normal)
        setTitleColor(color, for: .normal)

        prepareView()

    }

    required convenience init?(coder aDecoder: NSCoder) {
        self.init(frame: .zero)
        prepareView()
        fatalError("init(coder:) has not been implemented")

    }

    // MARK: - 创建视图
    fileprivate func prepareView() {

    }

    override func layoutSubviews() {
        super.layoutSubviews()

        guard bounds.isEmpty == false else {
            return
        }

        k_resizeSubviews()

        switch imagePosistion {
        case .top:
            k_layoutSubViewsForImagePositionTop()
        case .left:
            k_layoutSubViewsForImagePositionLeft()
        case .right:
            k_layoutSubViewsForImagePositionRight()
        case .bottom:
            k_layoutSubViewsForImagePositionBottom()
        }

    }

    fileprivate func k_resizeSubviews() {

        if let imageSize = imageSize {
            imageView!.size = imageSize
        } else if let image = image {
            imageView!.size = image.size
        }

        titleLabel!.sizeToFit()
        if imagePosistion == .right || imagePosistion == .left {
            if titleLabel!.width > (width - titleImageSpacing - imageView!.width) {
                titleLabel!.width = width
            }
        } else {
            if titleLabel!.width > width {
                titleLabel!.width = width
            }
        }
    }

    /// 图片在左侧

    func k_layoutSubViewsForImagePositionLeft() {
        if contentHorizontalAlignment == .right {
            // 整体靠右
            titleLabel!.x = width - titleLabel!.width
            titleLabel!.y = (height - titleLabel!.height) * 0.5
            imageView!.x = width - titleLabel!.width - titleImageSpacing - imageView!.width
            imageView!.y = (height - imageView!.height) * 0.5
        } else if contentHorizontalAlignment == .left {
            // 整体靠左
            imageView!.x = 0
            imageView!.y = (height - imageView!.height) * 0.5
            titleLabel!.x = imageView!.right + titleImageSpacing
            titleLabel!.y = (height - titleLabel!.height) * 0.5
        } else if contentHorizontalAlignment == .center {
            // 整体居中
            imageView!.x = width * 0.5 - (titleLabel!.width + titleImageSpacing + imageView!.width) * 0.5
            imageView!.y = (height - imageView!.height) * 0.5
            titleLabel!.x = titleImageSpacing + imageView!.right
            titleLabel!.y = (height - titleLabel!.height) * 0.5
        }
    }

    /// 图片在右侧
    func k_layoutSubViewsForImagePositionRight() {
        if contentHorizontalAlignment == .right {
            // 整体靠右
            imageView!.x = width - imageView!.width
            imageView!.y = (height - imageView!.height) * 0.5
            titleLabel!.x = width - imageView!.width - titleImageSpacing - titleLabel!.width
            titleLabel!.y = (height - titleLabel!.height) * 0.5
        } else if contentHorizontalAlignment == .left {
            // 整体靠左
            titleLabel!.x = 0
            titleLabel!.y = (height - titleLabel!.height) * 0.5
            imageView!.x = titleImageSpacing + titleLabel!.width
            imageView!.y = (height - imageView!.height) * 0.5
        } else if contentHorizontalAlignment == .center {
            // 整体居中
            titleLabel!.x = width * 0.5 - (titleLabel!.width + titleImageSpacing + imageView!.width) * 0.5
            titleLabel!.y = (height - titleLabel!.height) * 0.5
            imageView!.x = titleImageSpacing + titleLabel!.width
            imageView!.y = (height - imageView!.height) * 0.5
        }
    }

    /// 图片在顶部

    func k_layoutSubViewsForImagePositionTop() {
        if contentVerticalAlignment == .top {
            // 整体靠顶部
            imageView!.y = 0
            imageView!.center.x = width * 0.5
            titleLabel!.y = imageView!.bottom + titleImageSpacing
            titleLabel!.center.x = width * 0.5
        } else if contentVerticalAlignment == .bottom {
            // 整体靠底部
            titleLabel!.y = height - titleLabel!.height
            titleLabel!.center.x = width * 0.5
            imageView!.y = height - (imageView!.height + titleLabel!.height + titleImageSpacing)
            imageView!.center.x = width * 0.5
        } else if contentVerticalAlignment == .center {
            // 整体居中
            imageView!.y = height * 0.5 - (imageView!.height + titleLabel!.height + titleImageSpacing) * 0.5
            imageView!.center.x = width * 0.5
            titleLabel!.y = imageView!.bottom + titleImageSpacing
            titleLabel!.center.x = width * 0.5
        }
    }

    /// 图片在底部
    func k_layoutSubViewsForImagePositionBottom() {
        if contentVerticalAlignment == .top {
            // 整体靠顶部
            titleLabel!.y = 0
            titleLabel!.center.x = width * 0.5
            imageView!.y = titleLabel!.bottom + titleImageSpacing
            imageView!.center.x = width * 0.5
        } else if contentVerticalAlignment == .bottom {
            // 整体靠底部
            imageView!.y = height - imageView!.height
            imageView!.center.x = width * 0.5
            titleLabel!.y = height - (titleLabel!.height + titleImageSpacing + imageView!.height)
            titleLabel!.center.x = width * 0.5
        } else if contentVerticalAlignment == .center {
            // 整体居中
            titleLabel!.y = height * 0.5 - (imageView!.height + titleLabel!.height + titleImageSpacing) * 0.5
            titleLabel!.center.x = width * 0.5
            imageView!.y = titleLabel!.bottom + titleImageSpacing
            imageView!.center.x = width * 0.5
        }
    }

}
